package com.csd.controller;/*
 * Description:
 * User:Hello，Mr.Zhou
 * Date:2020-12-24
 * Time:15:33
 *
 * */

import com.csd.common.JsonBean;
import com.csd.dto.ActionResult;
import com.csd.dto.UserResult;
import com.csd.pojo.Order;
import com.csd.pojo.SysUser;
import com.csd.service.OrderService;
import com.csd.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class OrderController {
    @Autowired(required = false)
    private OrderService orderService;

    /**
     * 查询所有订单
     * @param session
     * @return
     */
    @RequestMapping(value = "/getOrder",method = RequestMethod.GET)
    public UserResult getOrder(HttpSession session){
        SysUser user = (SysUser) session.getAttribute("user");
        if (user!=null){
            Integer id = user.getId();
            Order order = orderService.findAllOrder(id);
            return new UserResult(200,"order success",order,1);
        }else {
            return new UserResult(404,"请登录",null,0);
        }

    }

    /**
     * 添加订单
     * @param session
     * @param cid
     * @param getid
     * @param backid
     * @param oprice
     * @return
     */
    @RequestMapping("/addorder")
    @ResponseBody
    public ActionResult addOrder(HttpSession session, Integer cid, Integer getid, Integer backid, String oprice) {

        ActionResult actionResult = null;
        SysUser user = (SysUser) session.getAttribute("user");
        if(user == null){
            actionResult = JsonUtils.actionResult(200,"您未登录！",null,2);

        }else {

            try {
                orderService.addOrder(user.getId(),cid, getid, backid, oprice);

                actionResult = JsonUtils.actionResult(200,"预定成功！",null,1);
            } catch (Exception e) {
                actionResult = JsonUtils.actionResult(0,e.getMessage(),null,0);
            }
        }

        return actionResult;

    }
    /**
     * 删除订单
     * @param
     * @return
     */
    @RequestMapping(value = "/delOrder",method = RequestMethod.DELETE)
    @ResponseBody
    public ActionResult delOrder(Integer id) {
        System.out.println("删除:"+id);
        orderService.delOrder(id);
        return new ActionResult(200,"delete success",null,1);
    }


}
